<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: index == options1curr }"
            v-for="(item, index) in options1"
            :key="index"
            @click="options1Fn(index, item.min, item.max)"
            >{{ item.txt }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: index == options2curr }"
            v-for="(item, index) in options2"
            :key="index"
            @click="options2Fn(index, item.type)"
            >{{ item.txt }}
          </span>
        </li>
      </ul>
      <List :arr="goodsListShow" />
      <div class="load">
        <span> <img src="../assets/img/transaction.png" alt="" /></span>
        {{ isReachBottom ? "加载完毕" : "加载中..." }}
      </div>
    </div>
  </div>
</template>                            
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { ProductsAPI } from "../request/api";
//  getScrollTop(),getClientHeight(),getScrollHeight()
import { getScrollTop, getClientHeight, getScrollHeight } from "../utils/index";
export default {
  data() {
    return {
      goodsList: [],
      did: 0, //栏目
      type: 0, //实体商品
      min: 0, //最小积分
      max: 0, //最大积分
      keyword: "", //关键字
      //排序条件列表数据
      options1: [
        { min: 0, max: 100000, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      //当前高亮的索引
      options1curr: 0,
      options2: [
        { type: 0, txt: "全部" },
        { type: 1, txt: "实体商品" },
        { type: 2, txt: "虚拟商品" },
      ],
      //当前高亮的索引
      options2curr: 0,
      // 当前所展示的8张图
      goodsListShow: [],
      page: 1, //当前页数
      size: 8, //当前可以显示8张
      isReachBottom: false,
      // 变量  是否正在加载数据
      isloading: false,
      // 默认没有加载到底部
      isReachBottom: false,
    };
  },
  created() {
    // query接收
    console.log("拿关键字", this.$route.query.keyword);
    this.keyword = this.$route.query.keyword;
    this.goodsSearch();
  },

  //   封装函数
  methods: {
    async goodsSearch() {
      let res = await ProductsAPI({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      console.log("res产品信息", res);
      this.goodsList = res.data;
      // 当前的8张图的长度赋值给最初的那个数组
      this.goodsListShow = this.goodsList.filter((item, index) => index < 8);

      // 查询方法初始化
      this.page = 1;
      this.isReachBottom = false; //没有加载到底部
      // 在判断这两个数组的长度是一样的;
      if (this.goodsList.length == this.goodsListShow.length) {
        this.isReachBottom = true;
        return;
      }
    },
    options1Fn(idx, min, max) {
      this.options1curr = idx;
      this.min = min;
      this.max = max;
      this.goodsSearch();
    },
    options2Fn(idx, type) {
      this.options2curr = idx;
      this.type = type;
      this.goodsSearch();
    },
    scrollFn() {
      console.log(
        "滚动了..",
        getScrollTop(),
        getClientHeight(),
        getScrollHeight()
      );

      if (getScrollTop() + getClientHeight() >= getScrollHeight() - 200) {
        //   console.log("到达底部了");
        console.log("可以加载新数据了..");
        // 判断数据是否加载到底部  两个长度保持一致
        if (this.goodsList.length == this.goodsListShow.length) {
          this.isReachBottom = true;
          return;
        }
        //   提取goodslist到goodsListShow

        /*      设定一个变量 让他不再继续加载 this.isloading == false
   如果现在没有在加载的话,我就可以往下执行   可以继续加载了
           一进来的时候  this.isloading = true  表示就开始开始了
        */
        if (this.isloading == false) {
          this.isloading = true; //表示可以开始加载了
          // 为false 的时候 就说明 可以开始加加了
          this.page++;
          setTimeout(() => {
            console.log("setTimeout");
            for (
              let i = this.size * (this.page - 1);
              i < this.page * this.size;
              i++
            ) {
              console.log("要加载的数据为...", this.page, this.goodsList[i]);
              //  this.goodsList[i] && this.goodsListShow.push(this.goodsList[i]);
              if (this.goodsList[i]) {
                this.goodsListShow.push(this.goodsList[i]);
              }
            }
            // 当前面加载完毕了  说说明没有加载了
            this.isloading = false; //现在是没有加载了, 回到循环的就开始 ,就说明又可以开始加载了...为true
          }, 1500);
        }
      }
    },
  },
  /*   watch: {4
    "$route.query.keyword": {
      handler(newVal, oldVal) {
        this.keyword = newVal;
        this.goodsSearch();
      },
    },
  }, */
  //   因为滚动的时候  全部商品页面滚动的会有滚动加载,去到首页滚动的时候也会进行加载
  mounted() {
    window.addEventListener("scroll", this.scrollFn);
  },
  //   销毁是为了消除在首页也会加载的bug
  beforeDestroy() {
    window.removeEventListener("scroll", this.scrollFn);
  },
  components: {
    Crumb,
    List,
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
  .load {
    text-align: center;
    margin-top: 15px;
    span {
      display: inline-block;
      animation: zhuan 2s linear infinite;
      img {
        vertical-align: middle;
      }
    }
  }
  @keyframes zhuan {
    0% {
      transform: rotate(0deg);
    }
    100% {
      transform: rotate(360deg);
    }
  }
}
</style>